﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using AutoMapper;
using KSUNO_EMS.Models;

namespace KSUNO_EMS.Controllers
{
    public class EmployeeProfileController : EmsController
    {
        //
        // GET: /EmployeeProfile/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Get()
        {
            var id = MvcApplication.GetCurrentUserInfo().Id;
            return new ProperJsonResult() {HasDates = true,
                Data = new AjaxResult(true,
                    Mapper.Map<EmployeeProfileDTO>(Database.Employees.Include(e=>e.Availability)
                    .SingleOrDefault(e=>e.Id== id)))};
        }
        public ActionResult Save(EmployeeProfileDTO employee)
        {
            try
            {
                var id = MvcApplication.GetCurrentUserInfo().Id;
                var dbEmployee = Database.Employees
                    .SingleOrDefault(e => e.Id == id);
                Mapper.Map(employee, dbEmployee);
                Database.SaveChanges();
                return new ProperJsonResult() { Data = new AjaxResult(true) };
            }
            catch (Exception ex)
            {

                return new ProperJsonResult() { Data = new AjaxResult(false, ex.ToString()) };
                ;
            }
        }
    }

}
